<template>
    <div class="cmt-container">
        <h3>发表评论</h3>
        <hr>
        <textarea v-model="commentContent" name="" id="" cols="30" rows="5" placeholder="请输入评论内容（120字）" maxlength="120"></textarea>
        <mt-button @click="postComment" type="primary" size="large">发表评论</mt-button>
        <div class="cmt-list">
            <div class="cmt-item" v-for="(item,index) in comments" :key="item.index">
                <div class="cmt-title">
                    第{{index + 1}}楼&nbsp;&nbsp;用户：{{item.user_name}}&nbsp;&nbsp;发表时间：{{item.add_time | dateFormat}}
                </div>
                <div class="cmt-body">{{item.content}}</div>
            </div>
        </div>
        <mt-button type="danger" size="large" plain @click="getMore">加载更多</mt-button>
    </div>
</template>
<script type="text/ecmascript-6">
    import {Toast} from "mint-ui"
    export default{
        data(){
            return {
                pageIndex: 1,
                comments: [],
                commentContent: ""//评论的内容
            }
        },
        created(){
          this.getComments();
        },
        methods: {
            getComments(){
                this.$http.get("comment/comment-" + this.id + ".json?pageIndex=" + this.pageIndex).then( result => {
                    if(result.body.status === 0){
//                        this.comments = result.body.message;
                        this.comments = this.comments.concat(result.body.message)
                    }else{
                        Toast("获取评论数据失败")
                    }
                })
            },
            getMore(){
                this.pageIndex ++;
                console.log(this.pageIndex);
                this.getComments();
            },
            postComment(){
                //发表评论
                if(this.commentContent.trim().length === 0){
                    return Toast("评论内容不能为空")
                }
                this.$http.get("postComment/postComment-" + this.$route.params.id  + ".json",{
                    commentContent: this.commentContent.trim()
                }).then(function(result){
                    if(result.body.status === 0){
                        var cmt = {
                            user_name: "匿名用户",
                            add_time: Date.now(),
                            content: this.commentContent.trim()
                        };
                        this.comments.unshift(cmt);
                        this.commentContent = "";
                    }else{
                        console.log("error");
                    }
                })
            }
        },
        props: ["id"]
    }
</script>
<style lang="less">
    .cmt-container{
        h3{
            font-size: 18px;
        }
        textarea{
            font-size: 14px;
            margin: 0;
        }
        .cmt-list{
        margin: 5px 0;
            .cmt-item{
                font-size: 13px;
                .cmt-title{
                    background-color: #ccc;
                    line-height: 30px;
                }
                .cmt-body{
                    line-height: 30px;
                    text-indent: 2em;
                }
            }
        }
    }
</style>